package cn.itcast.service.impl;

import cn.itcast.mapper.classsifyMapper;
import cn.itcast.pojo.classsify;
import cn.itcast.pojo.classsifyExample;
import cn.itcast.service.searchOrClassifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 辉
 * @title: searchOrClassifyServiceImpl
 * @projectName wx_shop
 * @description: TODO
 * @date 2019/5/3111:05
 */
@Slf4j
@Service
public class searchOrClassifyServiceImpl implements searchOrClassifyService {

    @Resource
    classsifyMapper classsifyMapper;




    @Override
    public List<Map<String,Object>> findAllClassify() {
        classsifyExample cEx = new classsifyExample();
        classsifyExample.Criteria criteria = cEx.createCriteria();
        criteria.andFidEqualTo(0);
        List<classsify> classsifies = classsifyMapper.selectByExample(cEx);

        List<Map<String,Object>> classifyList = new ArrayList<>();
        //前端使用数据结构

        for (int i = 0 ;i <classsifies.size();i++){
            Map<String,Object> classifyMap = new HashMap<String,Object>();
            classifyMap.put("text",classsifies.get(i).getName());
            classsifyExample cE = new classsifyExample();
            classsifyExample.Criteria cr = cE.createCriteria();
            cr.andFidEqualTo(classsifies.get(i).getId());
            List<classsify> classsifies1 = classsifyMapper.selectByExample(cE);
            for (int j = 0; j<classsifies1.size();j++){
                classsifies1.get(j).setText(classsifies1.get(j).getName());
            }
            classifyMap.put("children",classsifies1);
            classifyList.add(classifyMap);
        }




        return classifyList;
    }

    @Override
    public List<classsify> findOneClassify(int fid) {

        classsifyExample cEx = new classsifyExample();
        classsifyExample.Criteria criteria = cEx.createCriteria();
        criteria.andFidEqualTo(fid);
        List<classsify> classsifies = classsifyMapper.selectByExample(cEx);

        return classsifies;
    }

    @Override
    public classsify findOne(Integer id) {

        return classsifyMapper.selectByPrimaryKey(id);
    }
}
